﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.Data;
using HA.ModFlow.Domain.DomainLayer;
namespace HA.ModFlow.Controls.Windows.DataOperation
{
    /// <summary>
    /// winLayerErrorModifier.xaml 的交互逻辑
    /// </summary>
    public partial class winLayerErrorModifier : Window
    {
        public winLayerErrorModifier(GridData grid)
        {
            InitializeComponent();
            this._arryElev = grid.Elevations;
            this._grid = grid;
        }
        GridData _grid;
        List<int[,]> ibound; 

        List<double[,]> _arryElev = new List<double[,]>();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {          

            List<int[,]> ibounds = this._grid.WaterGrid.GetIBounds();
             ibound = new List<int[,]>();
            for (int i = 0; i < ibounds.Count; i++)
            {
                ibound.Add((int[,])ibounds[i].Clone());
            }

            GetCheckData();

        }

        private void btnFixSelected_Click(object sender, RoutedEventArgs e)
        {
            int mIndex = this.lstErrors.SelectedIndex;
            if (this._arryElev.Count == mIndex + 1)
            {
            }
            else
            {
                try
                {
                    bool b = false;



                    if (this.chkAverage.IsChecked.Value)
                    {
                        FixLayerError.Average(this._arryElev.GetRange(mIndex, 2), Convert.ToDouble(this.txtMin.Text),ref ibound);
                        b = true;
                    }
                    if (this.chkPreserveBottom.IsChecked.Value)
                    {
                        FixLayerError.ModefyBottom(this._arryElev.GetRange(mIndex, 2), Convert.ToDouble(this.txtMin.Text),ref ibound);
                        b = true;
                    }
                    if (this.chkPreserveTop.IsChecked.Value)
                    {
                        FixLayerError.ModifyUp(this._arryElev.GetRange(mIndex, 2), Convert.ToDouble(this.txtMin.Text), ref ibound);
                        b = true;
                    }
                    if (this.chkTruncate.IsChecked.Value)
                    {
                        FixLayerError.TruncateBedrock(this._arryElev, Convert.ToDouble(this.txtMin.Text), ref ibound);
                        b = true;
                    }
                    if (b)
                    {
                        this._grid.Elevations = this._arryElev;
                        //this._grid.WaterGrid.SetIBounds(ibound);
                    }
                    GetCheckData();
                }
                catch { }
            }
        }

        private void GetCheckData()
        {
            List<int> errors = new List<int>();
            errors = FixLayerError.GetErrorNum(this._arryElev, ibound,Convert.ToDouble(this.txtMin.Text));
            this.lstErrors.Items.Clear();
            for (int i = 0; i < this._arryElev.Count - 1; i++)
            {
                this.lstErrors.Items.Add("Layer " + (i + 1).ToString() + " (" + errors[i] + " errors)");
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            //List<int[,]> ibounds = this._grid.WaterGrid.GetIBounds();
            //List<int[,]> ibounds2 = new List<int[,]>();
            //for (int i = 0; i < ibounds.Count; i++)
            //{
            //    ibounds2.Add((int[,]) ibounds[i].Clone());
            //}
            //this._grid.se
            ////FixLayerError.GetIBound(this._arryElev, Convert.ToDouble(this.txtMin.Text), ref  ibounds2);
            //if (ibound.Count > 0)
            //{
            //    FixLayerError.GetIBound(this._arryElev, Convert.ToDouble(this.txtMin.Text),ref  this .ibound );
            //    this._grid.WaterGrid.SetIBounds(ibound);
            //}
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (ibound.Count > 0)
            {
                FixLayerError.GetIBound(this._arryElev, Convert.ToDouble(this.txtMin.Text), ref  this.ibound);
                this._grid.WaterGrid.SetIBounds(ibound);
            }
       
        }
    }
}
